package com.lepu.app.fun.health.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.lepu.app.fun.health.activity.DurgDetailActivity;
import com.lepu.app.fun.health.bean.BeanDurg.TopDurg;
import com.lepu.pasm.R;

public class MoreDurgAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<TopDurg> mArrayList = null;

	public MoreDurgAdapter(Context context,ArrayList<TopDurg> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}
	


	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.health_more_durg_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView.findViewById(R.id.mainItemLayout);
			ImageView iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
			TextView priceTextView = (TextView) convertView.findViewById(R.id.priceTextView);
			TextView titleTextView = (TextView) convertView.findViewById(R.id.titleTextView);

			holder.mainItemLayout = mainItemLayout;
			holder.iconImageView = iconImageView;
			holder.titleTextView = titleTextView;
			holder.priceTextView = priceTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final TopDurg item = mArrayList.get(position);
		
		if(item.ImgUrl != null){
			List<String> ImgUrls = (List<String>) Arrays.asList(item.ImgUrl.split("\\|"));
			UniversalImageLoadTool.disPlay(ImgUrls.get(0), holder.iconImageView,R.drawable.default_icon_small);
		}
		

		holder.titleTextView.setText(item.GenericName);
		holder.priceTextView.setText("￥"+item.Price);

		holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext,DurgDetailActivity.class);
				Bundle bundle1 = new Bundle();
				bundle1.putSerializable("durgId", item.MedicineID);
				intent.putExtras(bundle1);
				((BaseFragmentActivity)mContext).startActivity(intent,true);
			}
		});

		return convertView;
	}

	private static class ViewHolder {
		LinearLayout mainItemLayout;
		ImageView iconImageView;
		TextView titleTextView;
		TextView priceTextView;
	}
}
